

#include <stdio.h>
#include <stdlib.h>


#pragma GCC diagnostic ignored "-Wold-style-cast"

unsigned char counter = 0;
#define MAX_BUFFER 3
char test_array[MAX_BUFFER][100];

void test_case1() {

    for (int i = 0; i < 300; i++) {
        
        printf("%u - %u:", counter, (unsigned char)(counter - 1));
        counter++;
    }
}

void test_case2() {

    for (int i = 0; i < 300; i++) {
        //printf("%ld", (0x55cc46231060 - 0x55cc46230ffc)); 
        // 100, counter - 1 == -1
        char *ptr = test_array[(counter - 1) % MAX_BUFFER];
        printf("%p-%u\n", ptr, counter);
        counter++;
    }

}

void test_case3() {

   // int array[] = {1,2,3};
    
   // printf("%p - %p", array,  &array[-1]);
}

void test_case4() {

    int array[] = {1,2,3};
    counter = 0;

    for (int i = 0; i < 300; i++) {
        //char *ptr = test_array[(unsigned char)((counter - 1)) % MAX_BUFFER];
        int value = array[(counter++) % MAX_BUFFER];
        printf("%d-%u\n", value, counter);
    }  
}

void test_case5() {
    
    counter = 0;
    for (int i = 0; i < 300; i++) {
        char *ptr = test_array[(unsigned char)((counter - 1)) % MAX_BUFFER];
        printf("%p-%u\n", ptr, counter);
        counter++;
    }
}

void test_case6() {

    unsigned int i = 0xFFFFFFFF;
    unsigned int j, k;

    printf("%u-%u\n", i, i%3);
    j = i - 1;
    printf("%u-%u\n", j, j%3);
    k = i + 1;
    printf("%u-%u\n", k, k%3);

}

int buffer_counter = 0;

int get_prev_buffer_counter() {
    
    if ((buffer_counter - 1) < 0) {
        return MAX_BUFFER - 1;
    } else {
        return buffer_counter - 1;
    }
}

int inc_buffer_counter() {

    if ((buffer_counter + 1) >= MAX_BUFFER) {
        buffer_counter = 0;
    } else {
        buffer_counter++;
    }
    return buffer_counter;
}

int get_cur_buffer_counter() {
    return buffer_counter;
}

void test_case7() {

    printf("initial value %d \n", buffer_counter);
    for (int i = 0; i < 100; i++) {
        printf("cur %d, prev %d\t", get_cur_buffer_counter(), get_prev_buffer_counter());
        //printf("cur %d \t", get_cur_buffer_counter());
        printf("inc %d \n", inc_buffer_counter());
        //inc_buffer_counter();
    }

}

int main() {

    //test_case2();
    //test_case3();
    //test_case4();
    //test_case5();
    //test_case6();
    test_case7();
    return 0;
}